package com.hnust.aopdemo.security;

import com.hnust.aopdemo.service.AuthService;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 切面实例
 */
@Aspect
@Component
public class SecurityAspect {

    @Autowired
    private AuthService authService;

    /**
     * 切带有AdminOnly注解的方法
     */
    @Pointcut("@annotation(AdminOnly)")
    public void adminOnly() {

    }

    /**
     * advice:在adminOnly()方法之前执行
     */
    @Before("adminOnly()")
    public void check() {
        authService.checkAccess();
    }
}
